// 自定义事件
/**
 * 例子
 * v-tap="clickFun" 无参数
 * v-tap="{'fn':clickFun,key:value}" 有参数
 * function clickFun(obj){
 *  let value = obj.key;
 * }
 */
function dragFun(el, binding) {
  //   触屏函数
  var _this = this
  this.binding = binding
  this.myCallBack = typeof binding.value === 'object' ? binding.value.fn : binding.value
  this.binding.value = typeof binding.value === 'object' ? binding.value : {}
  el.addEventListener(
    'touchstart',
    function(e) {
      _this.start(e)
    },
    false
  )
  el.addEventListener(
    'touchend',
    function(e) {
      _this.end(e)
    },
    false
  )
  el.addEventListener(
    'touchmove',
    function(e) {
      _this.move(e)
    },
    false
  )
}
dragFun.prototype = {
  start: function(e) {
    // 监听touchstart事件
    this.binding.value.isStart = true
    this.binding.value.isMove = false
    this.binding.value.isEnd = false
    this.binding.value.startTouches = { x: e.changedTouches[0].pageX, y: e.changedTouches[0].pageY }
    this.myCallBack(this.binding.value, e)
  },
  move: function(e) {
    // 监听touchmove事件
    this.binding.value.isStart = false
    this.binding.value.isMove = true
    this.binding.value.isEnd = false
    this.binding.value.disX = e.changedTouches[0].pageX - this.binding.value.startTouches.x // 计算移动的位移差
    this.binding.value.disY = e.changedTouches[0].pageY - this.binding.value.startTouches.y
    this.binding.value.moveTouches = { x: e.changedTouches[0].pageX, y: e.changedTouches[0].pageY }
    this.myCallBack(this.binding.value, e)
  },
  end: function(e) {
    // 监听touchend事件
    this.binding.value.disX = e.changedTouches[0].pageX - this.binding.value.startTouches.x // 计算移动的位移差
    this.binding.value.disY = e.changedTouches[0].pageY - this.binding.value.startTouches.y
    this.binding.value.endTouches = { x: e.changedTouches[0].pageX, y: e.changedTouches[0].pageY }
    this.binding.value.isStart = false
    this.binding.value.isMove = false
    this.binding.value.isEnd = true
    this.myCallBack(this.binding.value, e)
  }
}

export default {
  // 点击事件
  bind: function(el, binding) {
    new dragFun(el, binding)
  }
}
